package com.chushouya.controller.common;

import com.chushouya.common.constant.*;
import com.chushouya.manager.constants.*;
import com.general.framework.core.enums.IEnum;
import com.general.framework.core.lang.Validate;
import com.general.framework.core.schema.Response;
import com.general.framework.core.schema.SelectModel;
import com.chushouya.order.constants.OrderListLogicStatusEnum;
import com.chushouya.order.constants.OrderQuickStatusEnum;
import com.chushouya.order.constants.PaymentMethodEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 枚举定义
 */
@Api(tags = "枚举定义")
@RestController
@RequestMapping(ServiceType.COMMON + "/enums")
@Slf4j
public class EnumsController {

    private static final Map<String, IEnum<?>[]> ENUMS = new HashMap<>();
    private static final Map<String, List<SelectModel<?>>> ALL_ENUMS = new HashMap<>();

    static {
        ENUMS.put("WebAppLoginTypeEnum", WebAppLoginTypeEnum.values());
        ENUMS.put("ConfigTypeEnum", ConfigTypeEnum.values());
        ENUMS.put("HandleStatusEnum", HandleStatusEnum.values());
        ENUMS.put("OssBasePathEnum", OssBasePathEnum.values());
        ENUMS.put("EvaluateCalcTypeEnum", EvaluateCalcTypeEnum.values());
        ENUMS.put("EvaluateItemSelectTypeEnum", EvaluateItemSelectTypeEnum.values());
        ENUMS.put("UserStatusEnum", UserStatusEnum.values());
        ENUMS.put("StoreStatusEnum", StoreStatusEnum.values());
        ENUMS.put("MerchantStatusEnum", MerchantStatusEnum.values());
        ENUMS.put("UserTypeEnum", UserTypeEnum.values());
        ENUMS.put("OrderQuickStatusEnum", OrderQuickStatusEnum.values());
        ENUMS.put("SettleBizTypeSimpleEnum", SettleBizTypeSimpleEnum.values());
        ENUMS.put("AccountUserTypeEnum", AccountUserTypeEnum.values());
        ENUMS.put("OrderListLogicStatusEnum", OrderListLogicStatusEnum.values());
        ENUMS.put("PaymentMethodEnum", PaymentMethodEnum.values());


        for (Map.Entry<String, IEnum<?>[]> entry : ENUMS.entrySet()) {
            ALL_ENUMS.put(entry.getKey(), getValues(entry.getValue()));
        }
    }

    @ApiOperation("获取所有枚举列表")
    @GetMapping("/getAllEnums")
    public Response<Map<String, List<SelectModel<?>>>> getAllEnums() {
        return Response.success(ALL_ENUMS);
    }

    @ApiOperation("获取枚举值列表")
    @GetMapping("/getEnum")
    public Response<List<SelectModel<?>>> getEnum(String enumName) {
        Validate.isFalse(ALL_ENUMS.containsKey(enumName), "枚举不存在");
        return Response.success(ALL_ENUMS.get(enumName));
    }

    private static List<SelectModel<?>> getValues(IEnum<?>[] values) {
        return Arrays.stream(values).map(v -> new SelectModel<>(v.value(), v.named())).collect(Collectors.toList());
    }


}
